× Lesson 1 Lesson 2 Lesson 3 Lesson 4 Lesson 5 Lesson 6 Lesson 7 Lesson 8 Lesson 9 Lesson 10 Lesson 11 Lesson 12 Lesson 13 Lesson 14 Lesson 15 Lesson 16 Lesson 17 Lesson 18 Lesson 19 Lesson 20 Lesson 21 Lesson 22 Lesson 23 Lesson 24 Lesson 25 Mini Lesson 1 Mini Lesson 2 Mini Lesson 3 Mini Lesson 4 Mini Lesson 5

Lessons

Lesson 24 - Sets

Sets are another type of arrays, which are once again lists, sets, tuples, and dictionaries. The difference between sets and other types of arrays is that the data in sets aren't in a set order and there are no indexes, like list[0] or tuple[1]. Here's an example of a list:

a_set = {1, 2, 3}

print(a_set[1])

a_set[2] = 5

We first we create a set, then we try to output a specific index, however with sets we can not do this, and we try to set a specific index equal to a new value, but once again we can not access or reassign a specific index. We can, however, use the add() function for a single piece of data, or the update() function to update multiple pieces of data using a list.

We can also remove a specific item, not a specific index, from a list, using the remove() function. The clear() function will empty the set, and just like lists, you can use the set() function to create a set.